var express = require("express");
var app = express();
var code = require('./utils/code');
var db = require("./utils/db");

app.listen(3001, "192.168.31.136");
console.log("port 3001 已启动");


//设置跨域访问
// app.all('*', function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header("Access-Control-Allow-Headers", "X-Requested-With");
//     res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
//     res.header("X-Powered-By",' 3.2.1')
//     res.header("Content-Type", "application/json;charset=utf-8");
//     next();
// });

function send(res,ret){
	var str = JSON.stringify(ret);
	res.send(str)
}

app.get('/', function(req, res) {
	res.send("express test");
});

app.get('/login', function(req, res) {
	var service_id = req.query.service_id;
	//var service_id = "10001";
	db.is_user_exist(service_id, function(ret) {
		if (ret == true) {
			console.log("======登陆成功=====", {code: code.LOGIN_SUCCESS})
			send(res, {code: code.LOGIN_SUCCESS});
		} else {
			//注册用户
			db.insert_service_id(service_id, function(ret) {
				if (ret == true) {
					send(res, {code: code.REGISTER_SUCCESS});
				}
			});
		}
	})
})